package com.g4.shoppingsingle.controller;


import com.g4.shoppingsingle.common.Result;
import com.g4.shoppingsingle.pojo.DdProductInfo;
import com.g4.shoppingsingle.service.IDdProductInfoService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * <p>
 * 商品表 前端控制器
 * </p>
 *
 * @author admin
 * @since 2021-10-27
 */
@Slf4j
@RestController
//@RequestMapping("/shoppingsingle/ddProductInfo")
public class DdProductInfoController {

    @Autowired
    private IDdProductInfoService ddProductInfoService;

    @GetMapping("/shoppingsingle/ddProductInfo/{id}")
    @ApiOperation("跳转到商品详情页")
    public ModelAndView liebiao(@ApiParam(value = "商品id") @PathVariable Long id) {
        //ModelAndView 使用这个来做页面跳转
        ModelAndView mv = new ModelAndView();
        mv.setViewName("/xiangqing");
        mv.addObject("id", id);

        return mv;
    }

    /**
     * 分页查询某个分类下的商品
     *
     * @param pdType
     * @param pageNum
     * @return
     */
    @GetMapping("/shoppingsingle/ddProductInfo/getByCategoryId/{pdType}/{pageNum}")
    public PageInfo<DdProductInfo> getByCategoryId(@PathVariable Integer pdType, @PathVariable Integer pageNum) {
        log.info("接收到参数如下:");
        log.info("分类id--: " + pdType);
        log.info("第几页--: " + pageNum);
        int pageSize = 10;
        PageInfo<DdProductInfo> pageInfo = ddProductInfoService.pdName(pdType, pageNum, pageSize);
//        System.out.println(demo);
        System.out.println(pageInfo.getList());
        // 3.别忘了返回所有问题列表
        return pageInfo;
    }

//    //http://localhost:40001/search?keyword=mi
//    @GetMapping("/search")
//    public String search( String keyword){
//        return "search";
//    }

    /**
     * 商品查询
     *
     * @param keyword
     * @param pageNo
     * @param pageSize
     * @return
     * @throws Exception
     */
    //http://localhost:40001/views/search/米/1/15
    @GetMapping("/views/search/{keyword}/{pageNo}/{pageSize}")
    public Result search(@PathVariable("keyword") String keyword,
                         @PathVariable("pageNo") Integer pageNo,
                         @PathVariable("pageSize") Integer pageSize) throws Exception {
        if (keyword == null || keyword.length() == 0) {
            keyword = "小米";
        }
        if (pageNo < 1 || pageNo==null) {
            pageNo = 1;
        }
        if (pageSize < 1 || pageSize==null) {
            pageSize = 10;
        }
        List<Object> search = ddProductInfoService.search(keyword, pageNo, pageSize);

        return Result.success(search);
    }


}
